package com.starblink.android.basic.ext

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.annotation.LayoutRes
import androidx.core.view.doOnDetach
import androidx.core.view.marginTop
import com.drake.statelayout.StateLayout
import com.starblink.android.basic.R
import com.starblink.android.basic.ext.gone
import com.starblink.android.basic.ext.visible
import com.starblink.basic.log.SKLogger
import com.starblink.basic.style.view.round.RoundKornerRelativeLayout
import com.starblink.basic.util.log.YYLogUtils

/**
 * @param showImg 是否显示图片
 * @param title 标题
 * @param subTitle 副标题/内容
 * @param buttonTxt 按钮文字
 * @param marginTop 如果不为null 则设置距离父布局的顶部间距
 * @param marginBottom 如果不为null 则设置距离父布局的底部间距
 * @param showDividerDebug 开启分割线debug显示
 * @param centerParentViewHeight 想要设置当前空布局的高度 如果不为null 则空布局所有内容根据高度居中 这个需要特定条件（提前获取宽高）才能用！！延迟设置会有抖动
 * @param onBtnClick 按钮点击回调
 */
fun View.setEmptyViewForWishTemplate(
    showImg: Boolean = true,
    title: String? = null,
    subTitle: String? = null,
    buttonTxt: String? = null,
    marginTop: Int? = null,
    marginBottom: Int? = null,
    centerParentViewHeight: Int? = null,
    showDividerDebug: Boolean = false,
    onBtnClick: (() -> Unit)? = null
) {

    //动态设置高度

    if (centerParentViewHeight != null) {
        val ll = findViewById<LinearLayout>(com.starblink.basic.style.R.id.ll_empty)
        if (ll != null) {
            ll.height(centerParentViewHeight)
        } else {
            toastShowDev("找不到marginLayout的id")
        }
    }

    val icon = findViewById<ImageView>(com.starblink.basic.style.R.id.icon)
    val msg = findViewById<TextView>(com.starblink.basic.style.R.id.msg)
    val msgSub = findViewById<TextView>(com.starblink.basic.style.R.id.msg_sub)
    val emptyButton = findViewById<RoundKornerRelativeLayout>(com.starblink.basic.style.R.id.empty_button)
    val emptyButtonTxt = findViewById<TextView>(com.starblink.basic.style.R.id.empty_button_txt)
    val marginLayoutTop = findViewById<View>(com.starblink.basic.style.R.id.margin_layout_top)
    val marginLayoutBottom = findViewById<View>(com.starblink.basic.style.R.id.margin_layout_bottom)
    val divider1 = findViewById<View>(com.starblink.basic.style.R.id.divider_line_1)
    val divider2 = findViewById<View>(com.starblink.basic.style.R.id.divider_line_2)
    if (showDividerDebug) {
        divider1.visible()
        divider2.visible()
    } else {
        divider1.gone()
        divider2.gone()
    }

    if (marginLayoutTop != null) {
        if (marginTop != null) {
            marginLayoutTop.margin(topMargin = marginTop)
        }
    } else {
        toastShowDev("找不到marginLayoutTop的id")
    }
    if (marginLayoutBottom != null) {
        if (marginBottom != null) {
            marginLayoutBottom.margin(bottomMargin = marginBottom)
        }
    } else {
        toastShowDev("找不到marginLayoutBottom的id")
    }

    if (icon != null) {
        if (showImg) {
            icon.visible()
        } else {
            icon.gone()
        }
    } else {
        toastShowDev("找不到icon的id")
    }
    if (msg != null) {
        if (title == null) {
            msg.gone()
        } else {
            msg.visible()
            msg.text = title
        }
    } else {
        toastShowDev("找不到msg的id")
    }
    if (msgSub != null) {
        if (subTitle == null) {
            msgSub.gone()
        } else {
            msgSub.visible()
            msgSub.text = subTitle
        }
    } else {
        toastShowDev("找不到subTitle的id")
    }
    if (emptyButton != null && emptyButtonTxt != null) {
        if (buttonTxt == null) {
            emptyButton.gone()
        } else {
            if (onBtnClick == null) {
                emptyButton.gone()
            } else {
                emptyButton.visible()
                emptyButtonTxt.text = buttonTxt
                emptyButton.setOnClickListener {
                    onBtnClick.invoke()
                }
            }
        }
    } else {
        toastShowDev("找不到emptyButton/emptyButtonTxt的id")
    }

}

 


